<?php
class SiteInfo extends CActiveRecord {    
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
    
    public function getDbConnection()
    {
        return Yii::app()->frontdesk;
    }

    public function tableName()
    {
        return 'site_info';
    }
    
    public function rules() {
        return array(
            array('about_us, contact_company_name, contact_company_email, user_name, site_name, sub_domain','required'),
            array('contact_company_email','email'),
            array('custom_domain','match','pattern'=>'/^(([a-zA-Z0-9][a-zA-Z0-9_\-]*)(\.[a-zA-Z0-9][a-zA-Z0-9_\-]*)+)$/'),
            array('sub_domain','match','pattern'=>'/^[a-zA-Z0-9_\-]+$/'),
            array('sub_domain','subDomainExists'),
        );
    }
    
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'user' => array(self::BELONGS_TO, 'FrontDeskUser', 'user_id'),
        );
    }
    
    public function subDomainExists($attribute, $params) {
        if (!$this->hasErrors($attribute))
        {
            $site_info_model = self::model()->findByAttributes(array('sub_domain' => $this->sub_domain));
            if (!is_null($site_info_model) && $site_info_model->id != $this->id){
                $this->addError('sub_domain', 'This sub domain name existed. Please try another!');
                return false;
            }
        }
    }

    /**
     * Returns the attribute labels.
     * Attribute labels are mainly used in error messages of validation.
     * By default an attribute label is generated using {@link generateAttributeLabel}.
     * This method allows you to explicitly specify attribute labels.
     *
     * Note, in order to inherit labels defined in the parent class, a child class needs to
     * merge the parent labels with child labels using functions like array_merge().
     *
     * @return array attribute labels (name=>label)
     * @see generateAttributeLabel
     */
    public function attributeLabels() {
        return array(
            'about_us' => 'Content for About Us',
            'contact_company_name' => 'Company Name',
            'contact_company_email' => 'Company E-mail',
            'user_name' => 'Name',
            'site_name' => 'Site Name',    
        );
    }
    
    public function showAttributesHiddenFields($exceptAttributes=array())
    {
        $exceptAttributes = CMap::mergeArray($exceptAttributes,array('id','creation_datetime'));
        $attrs = $this->attributes;
        $str = '';
        foreach($attrs as $attr => $value)
        {
            if (!in_array($attr,$exceptAttributes))
            {
                if ($attr=='modules' && is_array($this->modules))
                { 
                    foreach($this->modules as $key => $module)
                        $str .= '<input type="hidden" id="SiteInfo_modules_'.$key.'" name="SiteInfo[modules][]" value="'.$module.'" class="site-info-field modules-field" />';    
                }
                elseif ($attr!='modules')
                    $str .= CHtml::activeHiddenField($this,$attr,array('class'=>'site-info-field'));
            }
        }
        
        return $str;
    }
}